package com.jammingpro.blog_system.service;

import com.jammingpro.blog_system.mapper.BlogInfoMapper;
import com.jammingpro.blog_system.model.BlogInfo;
import com.jammingpro.blog_system.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public List<BlogInfo> getBlogInfoList() {
        List<BlogInfo> blogs = blogInfoMapper.getBlogInfoList();
        for(BlogInfo blog : blogs) {
            blog.setShowTime(DateUtils.parseFormatDate(blog.getCreateTime()));
        }
        return blogs;
    }

    public BlogInfo getBlogInfoById(Integer id) {
        BlogInfo blogInfo = blogInfoMapper.getBlogInfoById(id);
        if(blogInfo == null) return null;
        blogInfo.setShowTime(DateUtils.parseFormatDate(blogInfo.getCreateTime()));
        return blogInfo;
    }

    public Boolean insertBlogInfo(BlogInfo blogInfo) {
        Integer ret = blogInfoMapper.insertBlogInfo(blogInfo);
        if(ret <= 0) return false;
        return true;
    }

    public Integer getAuthorIdByBlogId(Integer blogId) {
        return blogInfoMapper.getAuthorIdByBlogId(blogId);
    }

    public Integer deleteBlogByBlogId(Integer blogId) {
        return blogInfoMapper.deleteBlogByBlogId(blogId);
    }

    public Boolean updateBlogInfo(BlogInfo blogInfo) {
        Integer ret = blogInfoMapper.updateBlogInfo(blogInfo);
        if(ret <= 0) return false;
        return true;
    }
}
